
package srzy

import java.io.File
import io.Source
import collection.mutable.ListBuffer

package object common {

  val isWindows = System.getProperty("os.name").matches("Windows[a-zA-Z0-9 ]*")

  def ifnull(s: String) = if (s == null) "" else s

  //  org.apache.log4j.PropertyConfigurator.configure("etc/logger.properties")
  //
  //  implicit def toLogger(o: Object) = new {
  //    def logger = org.apache.log4j.Logger.getLogger(o.getClass())
  //  }

  def getSysTime(format: String) = {
    val now = java.util.Calendar.getInstance(java.util.Locale.CHINA)
    val time = now.getTimeInMillis()
    val formater = new java.text.SimpleDateFormat(format)
    formater.format(time)
  }

  def split(str: String, regx: String): List[String] = {
    val index = 0
    val matchList = List.empty[String]
    val r1 = regx.toString.r

    val l = r1.findAllMatchIn(str).toList.map(m => (m.start(0), m.end(0)))
    val l2 = l ::: List((str.length(), 0))
    val l3 = l2.foldLeft((ListBuffer.empty[String], 0))((a, b) => {
      a._1.append(str.subSequence(a._2, b._1).toString)
      (a._1, b._2)
    })
    l3._1.toList
  }

  //  def toUpperAndLower(s: String) = {
  //    s.toCharArray().toList.flatMap(x => {
  //      if (x.toString matches "[a-zA-Z]") {
  //        ("[" + x.toUpper + x.toLower + "]").toCharArray().toList
  //      } else {
  //        List(x)
  //      }
  //    }).mkString
  //  }

  def writeToFile(f: File, s: String, code: String): Unit = {
    if (!f.exists()) {
      val d = f.getParentFile()
      if (!d.exists()) {
        d.mkdirs()
      }
      f.createNewFile()
    }
    val pw = new java.io.PrintWriter(f, code)
    try {
      pw.write(s)
      pw.flush()
    } finally {
      pw.close
    }
  }
  def writeToFile(f: File, s: String): Unit = writeToFile(f, s, "UTF-8")
  def writeToFile(filePath: String, s: String): Unit = writeToFile(new File(filePath), s)
  def writeToFile(filePath: String, s: String, code: String): Unit = writeToFile(new File(filePath), s, code)

  def readFromFile(f: File, code: String): List[String] = {
    val t = Source.fromFile(f, code)
    val l = t.getLines.toList
    t.close
    l
  }
  def readFromFile(f: File): List[String] = readFromFile(f, "UTF-8")
  def readFromFile(fileName: String): List[String] = readFromFile(new File(fileName), "UTF-8")
  def readFromFile(fileName: String, code: String): List[String] = readFromFile(new File(fileName), code)

  def readFromFileAll(f: File, code: String): String = {
    val t = Source.fromFile(f, code)
    val l = t.getLines.toList mkString "\n"
    t.close
    l
  }
  def readFromFileAll(f: File): String = readFromFileAll(f, "UTF-8")
  def readFromFileAll(fileName: String): String = readFromFileAll(new File(fileName), "UTF-8")
  def readFromFileAll(fileName: String, code: String): String = readFromFileAll(new File(fileName), code)

  def readFromURL(url: String, code: String): String = {
    val f = io.Source.fromURL(url, code)
    val s = f.getLines.mkString("\n")
    f.close
    s
  }

  def getOrException[T](o: Option[T], s: String = "value"): T =
    o match {
      case Some(v) => v
      case None => throw new Exception(s + " is not assigned")
    }

}